layui.use(['layer', 'table', 'form', 'transfer', 'util', 'laydate'], function() {
	var table = layui.table;
	var $ = layui.jquery;
	var form = layui.form;
	var transfer = layui.transfer;
	var util = layui.util;
	var laydate = layui.laydate;

	laydate.render({
		elem: '#endTime',
		type: 'datetime',
		format: 'yyyy-MM-dd HH:mm:ss'
	});
	laydate.render({
		elem: '#beginTime',
		type: 'datetime',
		format: 'yyyy-MM-dd HH:mm:ss'
	});
	setTimeout(function() {

		//初始化表格数据
		table.render({
			elem: '#test2',
			url: globalData.server + 'student/getStudentAlljingli1',
			where: {

				'consultantID': globalData.getUid()
			},
			method: "post",
			toolbar: '#toolbarDemo',
			defaultToolbar: ['filter', 'print', 'exports'],
			title: '用户列表',
			page: true,
			id: 'testReload2',
			cols: [
				[{
						checkbox: true,
						fixed: true
					},
					{
						field: 'stuid',
						title: 'ID',
						sort: true,
						width: 60,
						align: 'center'
					},

					{
						field: 'suoshuzixunshi',
						title: '所属咨询师',
						align: 'center'
					}, {
						field: 'lururens',
						title: '录入人',
						align: 'center'
					},
					{
						field: 'sname',
						title: '姓名',
						align: 'center'
					},
					{
						field: 'age',
						title: '年龄',
						align: 'center',
					},
					{
						field: 'sex',
						title: '性别',
						align: 'center',
					},
					{
						field: 'phone',
						title: '手机号',
						align: 'center'
					},
					{
						field: 'vx',
						title: '微信',
						align: 'center'
					},
					{
						field: 'qq',
						title: 'QQ',
						align: 'center'
					},
					{
						field: 'sourceWebsite',
						title: '来源网站',
						align: 'center'
					},
					{
						field: 'sourceMode',
						title: '来源方式',
						align: 'center'
					},
					{
						field: 'sourceKeywords',
						title: '来源关键词',
						align: 'center'
					},
					{
						field: 'establishTime',
						title: '创建时间',
						align: 'center'
					},
					{
						field: 'onlineNotes',
						title: '在线备注',
						align: 'center'
					},
					{
						field: 'courseDirection',
						title: '课程方向',
						align: 'center'
					},
					{
						field: 'scoring',
						title: '打分',
						align: 'center'
					},
					{
						field: 'isEffective',
						title: '是否有效',
						align: 'center'
					},
					{
						field: 'invalidTime',
						title: '无效时间',
						align: 'center'
					},
					{
						field: 'isReturnvisit',
						title: '是否回访',
						align: 'center'
					},
					{
						field: 'firstvisitTime',
						title: '首访时间',
						align: 'center'
					},
					{
						field: 'isDoor',
						title: '是否上门',
						align: 'center'
					},
					{
						field: 'depositAmount',
						title: '定金金额',
						align: 'center'
					},
					{
						field: 'depositTime',
						title: '定金时间',
						align: 'center'
					},
					{
						field: 'isPay',
						title: '是否缴费',
						align: 'center'
					},
					{
						field: 'payTime',
						title: '缴费时间',
						align: 'payMoney'
					},
					{
						field: 'isRefund',
						title: '是否退费',
						align: 'center'
					},
					{
						field: 'isInclass',
						title: '是否进班',
						align: 'center'
					},
					{
						field: 'inclassTime',
						title: '进班时间',
						align: 'center'
					},
					{
						field: 'inClassremarks',
						title: '进班备注',
						align: 'center'
					},
					{
						field: 'isDelete',
						title: '是否删除',
						align: 'center'
					},
					{
						fixed: 'right',
						title: '操作',
						toolbar: '#barDemo',
						width: 350,
						align: 'center'
					}
				]
			]
		});
		/*f复选框选择*/
		var onelist = [];
		var twolist = [];
		var type = null;
		//监听表格复选框选择
		table.on('checkbox(test2)', function(obj) {
			console.log(obj);
			console.log(obj.data.stuid);
			console.log(obj.type);
			if(obj.type == 'one') {
				type = 1;
				if(obj.checked == true) {
					layer.msg("one");
					onelist.push(parseInt(obj.data.stuid));
				} else {
					twolist.push(parseInt(obj.data.stuid));
				}
				console.log(onelist);
			} else if(obj.type == 'all') {
				type = 2;
				layer.msg("all");
			}
		});
		//重新加载
		function back() {
			table.reload('testReload', {
				page: {
					curr: $(".layui-laypage-em").next().html()
				}
			});
		}

		//模糊查询
		$('#search2').on('click', function() {
			table.reload('testReload2', {
				where: {
					'sname': $("#sname2").val(),
					'isPay': $("#isPay2").val(),
					'isEffective': $("#isEffective2").val(),
					'beginTime': $("#beginTime2").val(),
					'endTime': $("#endTime2").val(),
				}
			});
		});

		/*监听表头*/

		table.on('toolbar(test2)', function(obj) {
			/*switch(obj.event) {
				case 'addUser':
					layer.open({
						type: 2,
						area: ['500px', '400px'],
						content: 'AddStudent.html',
						title: '添加',
						end: function() {
							window.location.reload();
						}
					});
					break;
			};*/
			if(obj.event === 'addUser') { // 监听添加操作
				var index = layer.open({
					title: '添加用户',
					type: 2,
					shade: 0.2,
					maxmin: true,
					shadeClose: true,
					area: ['500px', '400px'],
					content: 'AddStudent.html',
						success : function(layero, index) {
							var body = layer.getChildFrame('body', index);
							body.find("#lururen").val(globalData.getUid());
								
								
						},
					end: function() {
						window.location.reload();
					}
				});
				$(window).on("resize", function() {
					layer.full(index);
				});
			} else if(obj.event === 'excel') {
				//询问框
				layer.confirm('确定导出表格吗？', {
					btn: ['确定', '取消'] //按钮
				}, function() {
					$.post(globalData.server + "student/excel", {
						"type": type,
						"one": onelist,
						"two": twolist
					}, function(res) {
						if(res.count > 0) {
							// 假如返回的 res.data 是需要导出的列表数据
							console.log(res.data); // [{name: 'wang', age: 18, sex: '男'}, {name: 'layui', age: 3, sex: '女'}]
							// 1. 数组头部新增表头
							res.data.unshift({
								sname: '用户名',
								sex: '男',
								age: '年龄'
							});
							// 2. 如果需要调整顺序，请执行梳理函数
							var data = LAY_EXCEL.filterExportData(res.data, [
								'sname',
								'sex',
								'age',
							]);
							// 3. 执行导出函数，系统会弹出弹框
							LAY_EXCEL.exportExcel({
								sheet1: data
							}, '学生信息.xlsx', 'xlsx');
						}
					}, "json");
				}, function() {

				});
			}
		});

		//监听行工具事件
		table.on('tool(test2)', function(obj) {
			var data = obj.data;
			if(obj.event === 'del') {
				layer.confirm('真的清除吗', function(index) {
					$.post(globalData.server + 'student/deleteStudent', {
						"stuid": data.stuid
					}, function(res) {
						if(res.count > 0) {
							layer.msg('成功啦', {
								icon: 1,
								time: 2000
							}, function() {
								back();
							});
						} else {
							layer.msg('失败啦', {
								icon: 5,
								time: 2000
							}, function() {
								back();
							});
						}
					})
					layer.close(index);
				});
			} else if(obj.event === 'zhuizong') {
				layer.open({
					title: '您正在追踪姓名为：' + obj.data.sname + '信息',
					type: 2,
					shade: 0.3,
					shadeClose: true,
					skin: 'layui-layer-molv',
					anim: 5,
					btnAlign: 'c',
					closeBtn: 1,
					area: ['500px', '500px'],
					content: 'mystudent-zhuizong.html',
					success: function(layero, index) {
						var body = layer.getChildFrame('body', index);
						body.find("#stuid").val(obj.data.stuid);
						body.find("#userid").val(globalData.getUid());
					},
					end: function() {
						back();
					}
				});
			} else if(obj.event === 'selectzhuizong') {
				layer.open({
					title: '您正在查看姓名为：' + obj.data.sname + '信息',
					type: 2,
					shade: 0.3,
					shadeClose: true,
					skin: 'layui-layer-molv',
					anim: 5,
					btnAlign: 'c',
					closeBtn: 1,
					area: ['800px', '500px'],
					content: 'mystudent-chakanzhuizong2.html',
					success: function(layero, index) {
						var body = layer.getChildFrame('body', index);
						body.find("#stuid").val(obj.data.stuid);
					},
					end: function() {
						back();
					}
				});
			} else if(obj.event === 'xiugai') {
				layer.open({
					title: '您正在修改姓名为：' + obj.data.sname + '信息',
					type: 2,
					shade: 0.3,
					shadeClose: true,
					skin: 'layui-layer-molv',
					anim: 5,
					btnAlign: 'c',
					closeBtn: 1,
					area: ['500px', '500px'],
					content: 'mystudent-update.html',
					success: function(layero, index) {
						var body = layer.getChildFrame('body', index);
						body.find("#stuid").val(obj.data.stuid);
						body.find("#sname").val(obj.data.sname);
						body.find("#qq").val(obj.data.qq);
						body.find("#vx").val(obj.data.vx);
						body.find("#phone").val(obj.data.phone);
						/*body.find("#consultantID").val(obj.data.consultantID);*/

					},
					end: function() {
						back();
					}
				});
			}
		});
	}, 1000);
});